	package view.manager;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import dao.DepartmentDao;
import dao.MajorDao;
import dao.ManagerDao;
import model.Department;
import model.Major;
import model.Manager;
import util.StringUtil;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TeacherEditInterfrm extends JInternalFrame {
	private JTextField textField;
	private JTable table;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JComboBox comboBox;
	private JRadioButton rdbtnNewRadioButton;
	private JRadioButton rdbtnNewRadioButton_1;
	private String issuper = "0";
	private final ButtonGroup buttonGroup = new ButtonGroup();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TeacherEditInterfrm frame = new TeacherEditInterfrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TeacherEditInterfrm() {
		setIconifiable(true);
		setClosable(true);
		setTitle("教师管理");
		setBounds(100, 100, 668, 512);

		JLabel lblNewLabel = new JLabel("教师姓名：");

		textField = new JTextField();
		textField.setColumns(10);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Manager> arrayList = new ArrayList<Manager>();
				ArrayList<Manager> List = new ArrayList<Manager>();
				arrayList = ManagerDao.select();
				for (Manager manager : arrayList) {
					if (manager.getT_name().contains(textField.getText())) {
						List.add(manager);
					}
				}
				filltable(List);
			}
		});
		btnNewButton.setIcon(new ImageIcon(TeacherEditInterfrm.class.getResource("/images/select.png")));

		JScrollPane scrollPane = new JScrollPane();

		JPanel panel = new JPanel();
		panel.setBorder(
				new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				/**
				 * b表格点击事件
				 */
				comboBox.removeAllItems();
				int row = table.getSelectedRow();
				textField_1.setText(table.getValueAt(row, 0).toString());
				textField_2.setText(table.getValueAt(row, 1).toString());
				textField_3.setText(table.getValueAt(row, 2).toString());
				fillTeacherType(String.valueOf(table.getValueAt(row, 3)));
				if ("0".equals(String.valueOf(table.getValueAt(row, 4)))) {
					rdbtnNewRadioButton_1.setSelected(true);
				} else {
					rdbtnNewRadioButton.setSelected(true);
				}
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
			},
			new String[] {
				"\u804C\u5DE5\u53F7", "\u59D3\u540D", "\u7535\u8BDD", "\u6240\u5728\u7CFB", "\u662F\u5426\u4E3A\u7BA1\u7406\u5458"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(1).setPreferredWidth(89);
		table.getColumnModel().getColumn(2).setPreferredWidth(95);
		table.getColumnModel().getColumn(3).setPreferredWidth(135);
		table.getColumnModel().getColumn(4).setPreferredWidth(87);
		scrollPane.setViewportView(table);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addGap(135).addComponent(lblNewLabel).addGap(18)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
						.addGap(102).addComponent(btnNewButton))
				.addGroup(groupLayout.createSequentialGroup().addGap(53).addComponent(scrollPane,
						GroupLayout.PREFERRED_SIZE, 552, GroupLayout.PREFERRED_SIZE))
				.addGroup(groupLayout.createSequentialGroup().addGap(53).addComponent(panel, GroupLayout.PREFERRED_SIZE,
						552, GroupLayout.PREFERRED_SIZE)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addGap(23)
				.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup().addGap(3).addComponent(lblNewLabel,
								GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup().addGap(2).addComponent(textField,
								GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnNewButton))
				.addGap(32).addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
				.addGap(18).addComponent(panel, GroupLayout.PREFERRED_SIZE, 219, GroupLayout.PREFERRED_SIZE)));

		JLabel lblNewLabel_1 = new JLabel("教师工号：");

		textField_1 = new JTextField();
		textField_1.setEditable(false);
		textField_1.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("教师姓名：");

		textField_2 = new JTextField();
		textField_2.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("教师电话：");

		textField_3 = new JTextField();
		textField_3.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("所在系：");

		comboBox = new JComboBox();

		JLabel lblNewLabel_5 = new JLabel("是否为管理员：");

		rdbtnNewRadioButton = new JRadioButton("是");
		rdbtnNewRadioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				issuper = "1";
			}
		});
		buttonGroup.add(rdbtnNewRadioButton);

		rdbtnNewRadioButton_1 = new JRadioButton("否");
		buttonGroup.add(rdbtnNewRadioButton_1);

		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "请选择需要修改的数据", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}
				
				Manager manager = ManagerDao.getManager(textField_1.getText());
				manager.setM_id(null);
				if (comboBox.getSelectedItem() instanceof Major) {
					Major major = (Major) comboBox.getSelectedItem();
					manager.setM_id(major.getM_id());
				}
				manager.setT_name(textField_2.getText());
				manager.setT_tel(textField_3.getText());
				manager.setIssuper(issuper);
				if (ManagerDao.updata(manager) > 0) {
					JOptionPane.showMessageDialog(null, "修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					filltable(ManagerDao.select());
					return;
				}
				JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(TeacherEditInterfrm.class.getResource("/images/updata.png")));

		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtil.isEmpty(textField_1.getText())) {
					JOptionPane.showMessageDialog(null, "请选择需要删除的数据", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}
				int res = JOptionPane.showConfirmDialog(null, "是否确定删除");
				if (res == 0) {
					if (ManagerDao.delete(textField_1.getText()) > 0) {
						JOptionPane.showMessageDialog(null, "删除成功", "提示", JOptionPane.INFORMATION_MESSAGE);
						textField_1.setText("");
						textField_2.setText("");
						textField_3.setText("");
						filltable(ManagerDao.select());
						return;
					}
					JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNewButton_2.setIcon(new ImageIcon(TeacherEditInterfrm.class.getResource("/images/删 除 .png")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup().addGap(30)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup().addComponent(lblNewLabel_1)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
										.addGap(90).addComponent(lblNewLabel_4).addGap(16)
										.addComponent(
												comboBox, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
										.addGap(43))
								.addGroup(gl_panel.createSequentialGroup().addComponent(lblNewLabel_3)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 120,
												GroupLayout.PREFERRED_SIZE)
										.addGap(88).addComponent(lblNewLabel_5).addGap(4)
										.addComponent(rdbtnNewRadioButton).addGap(6).addComponent(rdbtnNewRadioButton_1)
										.addGap(70))
								.addGroup(gl_panel.createSequentialGroup().addComponent(lblNewLabel_2)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 119,
												GroupLayout.PREFERRED_SIZE)
										.addContainerGap())))
				.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup().addGap(138).addComponent(btnNewButton_1)
						.addGap(120).addComponent(btnNewButton_2).addContainerGap(132, Short.MAX_VALUE)));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup()
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup().addContainerGap().addGroup(gl_panel
								.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1)
								.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_4))
								.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_panel.createSequentialGroup()
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
														.addComponent(lblNewLabel_2).addComponent(textField_2,
																GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
														.addComponent(lblNewLabel_3).addComponent(textField_3,
																GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)))
										.addGroup(gl_panel.createSequentialGroup().addGap(45)
												.addComponent(lblNewLabel_5))))
						.addGroup(gl_panel.createSequentialGroup().addGap(8)
								.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addGap(41)
								.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addComponent(rdbtnNewRadioButton).addComponent(rdbtnNewRadioButton_1))))
				.addPreferredGap(ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING,
								gl_panel.createSequentialGroup().addComponent(btnNewButton_1).addGap(23))
						.addGroup(Alignment.TRAILING,
								gl_panel.createSequentialGroup().addComponent(btnNewButton_2).addGap(22)))));
		panel.setLayout(gl_panel);
		getContentPane().setLayout(groupLayout);

		filltable(ManagerDao.select());
	}

	private void filltable(ArrayList<Manager> arrayList) {
		Major major;
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.setRowCount(0);
		for (Manager manager : arrayList) {
			Vector v = new Vector();
			v.add(manager.getT_id());
			v.add(manager.getT_name());
			v.add(manager.getT_tel());
			major = MajorDao.select(manager.getM_id());
			v.add(major.getM_name());
			v.add(manager.getIssuper());
			dtm.addRow(v);
		}
	}

	/**
	 * 初始化下拉框
	 */
	private void fillTeacherType(String firstComboBox) {
		ArrayList<Major> arrayList = MajorDao.select();
		if ("null".equals(firstComboBox)) {
			comboBox.addItem("请选择专业");
		}
		for (Major major : arrayList) {
			if (firstComboBox.equals(major.getM_name())) {
				comboBox.addItem(major);
			}
		}
		for (Major major : arrayList) {
			if (!firstComboBox.equals(major.getM_name())) {
				comboBox.addItem(major);
			}
		}
	}
}
